package dev.werillib.utils

import android.graphics.Bitmap
import android.util.Log.e
import android.util.Log.w
import java.io.FileOutputStream
import java.io.IOException

object BitmapUtil {
    fun saveImage(bitmap: Bitmap, absPath: String): Boolean {
        return saveBitmap(bitmap, absPath, 100)
    }

    fun saveBitmap(bitmap: Bitmap, absPath: String, quality: Int): Boolean {
        if (FileUtil.exists(absPath)) {
            FileUtil.delete(absPath)
        }

        if (!FileUtil.create(absPath)) {
            w("BitmapUtil", "create file failed.")
            return false
        } else {
            return try {
                val fos = FileOutputStream(absPath)
                bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos)
                fos.flush()
                fos.close()
                true
            } catch (var4: IOException) {
                var4.printStackTrace()
                e("BitmapUtil", "failed to write image content")
                false
            }

        }
    }
}
